import { ElNotification } from "element-plus";

//  页面跳转
export const $navigateTo = function $navigateTo(...args) {
  const { jumpType, page, urlLink, pageParams } =
    args[args.length - 1];
  const query = {};
  if (pageParams && pageParams.length > 0) {
    pageParams.forEach((item) => {
      // @ts-ignore
      query[item.name] = item.type === "express" ? `${item.value}` : item.value;
    });
  }
  if (jumpType === "page") {
    // @ts-ignore
    this.$router.push({
      path: page,
      query,
    });
  } else if (jumpType === "link") {
    window.open(urlLink);
  }
};
//  页面返回
export const $navigateBack = function $navigateBack() {
  // @ts-ignore
  this.$router.go(-1);
};
//  页面刷新
export const $refresh = function $refresh() {
  window.location.reload();
};
//  组件显隐
export const $showOrHide = function $showOrHide(...args) {
  const { componentId, hide } = args[args.length - 1];
  // @ts-ignore
  $getComponent.call(this, componentId).show(hide);
};
// 变量赋值
export const $setState = function $setState(...args) {
  // @ts-ignore
  Object.assign(this.state, args[args.length - 1]);
};
// 消息提示
export const $messageAlert = function $messageAlert(...args) {
  const { infoType, infoContent } = args[args.length - 1];
    ElNotification({
    message: infoContent,
    type:
      infoType === "notice"
        ? "Info"
        : infoType === "error"
        ? "Error"
        : (infoType ),
  });
};

//  调用数据源
export const $callDataSource = function $callDataSource(...args) {
  const { variableName, dataSourceId, successCallback, errorCallback, params } =
    args[args.length - 1];
  const newParams= {};
  // @ts-ignore
  for (const [key, value] of Object.entries(params)) {
    newParams[key] =
      typeof value === "object" && value.type === "JSExpression"
        ? `${value.value}`
        : value;
  }
  // @ts-ignore
  this.dataSourceMap[dataSourceId](newParams)
    .then((res) => {
      if (variableName) {
        // @ts-ignore
        $setState.apply(this, [
          {
            [variableName]: res,
          },
        ]);
      }
      if (successCallback) {
        successCallback();
      }
    })
    .catch(() => {
      if (errorCallback) {
        errorCallback();
      }
    });
};
/*export const $messageDialog = function $messageDialog(...args: any[]) {
  const {
    infoTitle,
    infoType,
    infoContent,
    okText,
    showCancel,
    successCallback,
    errorCallback,
  } = args[args.length - 1];
  ElMessageBox.confirm(infoContent, infoTitle, {
    confirmButtonText: okText,
    cancelButtonText: "取消",
    type: infoType,
    showCancelButton: showCancel,
  })
    .then(() => {
      if (successCallback) {
        successCallback();
      }
    })
    .catch(() => {
      if (errorCallback) {
        errorCallback();
      }
    });
};*/

// 步骤表单-跳转
export const $stepJump = function $stepJump(current) {
  // context.current = current;
  // console.log(arguments);
  // @ts-ignore
  this.state.current = current;
};
// 文件下载
export const $download = function $download(...args) {
  const { fileName, fileUrl } = args[args.length - 1];
  const a = document.createElement("a");
  a.style = "display: none"; // 创建一个隐藏的a标签
  a.href = typeof fileUrl === "string" ? fileUrl : `${fileUrl.value}`;
  a.download = fileName;
  document.body.appendChild(a);
  a.click(); // 触发a标签的click事件
  document.body.removeChild(a);
};
// 设置表单数据
export const $setData = function $setData(...args) {
  const { variableName, form, data } = args[args.length - 1];
  // @ts-ignore
  this.state[variableName] = { ...form, ...data };
};
// 获取组件
export const $getComponent = function $getComponent(ref) {
  // @ts-ignore
  return this.$refs[ref];
};
// 事件编排
export const $eventSchedule = function (...args) {
  const { eventCode } = args[args.length - 1];
  // @ts-ignore
  const event  = eventFlow.orchestrateMap[eventCode];
  // @ts-ignore
  event.execute(this);
};
export default {
  $navigateTo,
  $navigateBack,
  $refresh,
  $showOrHide,
  $setState,
  $messageAlert,
  $callDataSource,
  $stepJump,
  $download,
  $setData,
  $getComponent,
  // $messageDialog,
  $eventSchedule,
};
